package com.quectel.emp.controller.scheduling;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.scheduling.SchedulingApplyConstants;
import com.quectel.constant.core.scheduling.SchedulingTodoConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.schedulingapply.dto.SchedulingApplyRecordAuditPersonDto;
import com.quectel.core.module.schedulingapply.dto.SchedulingApplyRecordDto;
import com.quectel.core.module.schedulingapply.service.SchedulingApplyRecordAuditPersonService;
import com.quectel.core.module.schedulingapply.service.SchedulingApplyRecordService;
import com.quectel.core.module.workorder.dto.WorkOrderDto;
import com.quectel.core.module.workorder.service.WorkOrderService;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.emp.vo.scheduling.SchedulingTodoResp;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-26 16:41:00
 */
@RestController
@RequestMapping("schedulingTodo")
@Tag(name = "SchedulingTodoController", description = "员工待办相关api")
public class SchedulingTodoController extends BaseController {

    @DubboReference
    private WorkOrderService workOrderService;
    @DubboReference
    private DeptPersonService deptPersonService;
    @DubboReference
    private SchedulingApplyRecordService schedulingApplyRecordService;
    @DubboReference
    private SchedulingApplyRecordAuditPersonService auditPersonService;

    @GetMapping("todoList")
    @Operation(summary = "员工端待办事项类型type 字典值SchedulingTodoType: 1.工单 2.请假审批")
    public Response<List<SchedulingTodoResp>> todoList() {
        SessionHolder.checkEmp();
        DeptPersonDto deptPersonDto = Objects.requireNonNull(SessionHolder.getEmp());
        Long tenantId = deptPersonDto.getTenantId();
        Long villageId = deptPersonDto.getVillageId();

        List<SchedulingTodoResp> resultList = new ArrayList<>();

        // 工单
        CompletableFuture<Void> workOrder = CompletableFuture.runAsync(() -> {
            Map<String, Object> requestMap = new HashMap<>();

            //标记查询来源
            requestMap.put("empQuery", SystemConstants.YES.toString());
            requestMap.put("empQueryUserId", deptPersonDto.getId());
            requestMap.put(LambdaUtil.getFieldName(WorkOrderDto::getTenantId),tenantId);
            requestMap.put(LambdaUtil.getFieldName(WorkOrderDto::getVillageId), villageId);
            requestMap.put("statusFlag", 2);

            List<WorkOrderDto> workOrderDtos = workOrderService.queryList(requestMap);
            if (CollectionUtils.isNotEmpty(workOrderDtos)){
                SchedulingTodoResp resp = new SchedulingTodoResp();
                WorkOrderDto workOrderDto = workOrderDtos.get(0);
                resp.setId(workOrderDto.getId());
                resp.setType(SchedulingTodoConstants.TodoType.WORK_ORDER.getType());
                resp.setName(workOrderDto.getDetail());
                resp.setCreateTime(workOrderDto.getCreateTime());
                resultList.add(resp);
            }

        });

        /**
         * 这里审批信息弹给审批人；
         *      1.确认当前登录人在审批流程中是否存在
         */
        CompletableFuture<Void> schedulingAudit = CompletableFuture.runAsync(() -> {
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put(LambdaUtil.getFieldName(SchedulingApplyRecordAuditPersonDto::getAuditDeptPersonId),deptPersonDto.getId());
            int auditPersonTotal = auditPersonService.queryTotal(requestMap);
            if (auditPersonTotal > 0){
                auditPersonService.queryTotal(requestMap);
            }

            Map<String, Object> sqlMap = new HashMap<>();
            sqlMap.put(LambdaUtil.getFieldName(SchedulingApplyRecordDto::getTenantId),tenantId);
            sqlMap.put(LambdaUtil.getFieldName(SchedulingApplyRecordDto::getVillageId),villageId);
            sqlMap.put(LambdaUtil.getFieldName(SchedulingApplyRecordAuditPersonDto::getAuditDeptPersonId),deptPersonDto.getId());
            sqlMap.put(SchedulingApplyConstants.IS_CURRENT,"1");
            requestMap.put(LambdaUtil.getFieldName(SchedulingApplyRecordDto::getStatus), SystemConstants.NO);

            List<SchedulingApplyRecordDto> schedulingApplyRecordDtos = schedulingApplyRecordService.queryRecordList(sqlMap);
            if (CollectionUtils.isNotEmpty(schedulingApplyRecordDtos)){
                SchedulingApplyRecordDto schedulingApplyRecordDto = schedulingApplyRecordDtos.get(0);
                SchedulingTodoResp resp = new SchedulingTodoResp();
                resp.setId(schedulingApplyRecordDto.getId());
                resp.setType(SchedulingTodoConstants.TodoType.SCHEDULING_AUDIT.getType());

                SchedulingApplyConstants.ApplyTypeEnum byType
                        = SchedulingApplyConstants.ApplyTypeEnum.getByType(schedulingApplyRecordDto.getApplyType());
                String desc = byType.getDesc();
                String applyPersonName ;
                DeptPersonDto applyDeptPerson = deptPersonService.selectCacheById(schedulingApplyRecordDto.getApplyDeptPersonId());
                if (applyDeptPerson == null){
                    applyPersonName = "未找到提交人";
                }else {
                    applyPersonName = applyDeptPerson.getName();
                }
                resp.setName(String.format(SchedulingTodoConstants.SCHEDULING_AUDIT_TITLE, applyPersonName,desc));
                resp.setCreateTime(schedulingApplyRecordDto.getCreateTime());
                resultList.add(resp);
            }


        });


        CompletableFuture.allOf(workOrder, schedulingAudit).join();

        return Response.<List<SchedulingTodoResp>>ok().wrap(resultList);
    }
}
